﻿/// <reference name="MicrosoftAjax.js"/>

var cert = {
    check: function (id) {
        id = id.toLowerCase();
        var reg = /\d{18}|\d{17}x|\d{15}/gi;
        var obj = {
            c15: '',
            c18: '',
            sex: '',
            bir: '',
            msg: '身份证号输入不正确，请重新输入！'
        };
        if (reg.test(id)) {//通过基本验证
            if (id.length == 18 && id.charAt(17) == this.getcheck(id.substr(0, 17))) {
                //通过了校验位
                obj.c15 = this.get15From18(id);
                obj.c18 = id;
                obj.sex = this.getSex(id);
                obj.bir = this.getBir(id);
                obj.msg = '';
            }
            if (id.length == 15) {
                obj.c15 = id;
                obj.c18 = this.get18From15(id);
                obj.sex = this.getSex(id);
                obj.bir = this.getBir(id);
                obj.msg = '';
            }
        }
        return obj;
    },

    get15From18: function (id) {
        return id.substr(0, 6) + id.substr(8, 9);
    },
    get18From15: function (id) {
        id = id.substr(0, 6) + '19' + id.substr(6, 9);
        return id + this.getcheck(id);
    },
    getSex: function (id) {
        var f = ['女', '男'];
        var c = 11;
        if (id.length == 18) {
            c = id.charAt(16)
        }
        else if (id.length == 15) {
            c = id.charAt(14);
        }
        else {
            return '';
        }
        return f[c % 2];
    },
    getBir: function (id) {
        var bir = '';
        if (id.length == 18) {
            bir = id.substr(8, 6);
            return id.substr(6, 2) + bir.substr(0, 2) + '-' + bir.substr(2, 2) + '-' + bir.substr(4, 2);
        }
        else if (id.length == 15) {
            bir = id.substr(6, 6);
            return '19' + bir.substr(0, 2) + '-' + bir.substr(2, 2) + '-' + bir.substr(4, 2);
        }
        else {
            return '';
        }
    },
    getcheck: function (id) {
        var xishu = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
        var xiaoyan = [1, 0, 'x', 9, 8, 7, 6, 5, 4, 3, 2];
        var sum = 0;
        for (var i = 0; i < 17; i++) {
            sum += id.charAt(i) * xishu[i];
        }
        return xiaoyan[sum % 11];
    }

};